package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.dao.CourseDao;
import com.offcn.dao.CourseDaoImpl;
import com.offcn.entity.Course;
import com.offcn.service.Course_UserService;
import com.offcn.service.Course_UserServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

@WebServlet("/course_user")
public class Course_UserServlet extends BaseServlet {
       Course_UserService course_userService = new Course_UserServiceImpl();
       ObjectMapper objectMapper = new ObjectMapper();
       CourseDao courseDao = new CourseDaoImpl();
       ResultVo resultVo = null;
//       分页查询
    public  void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String search = req.getParameter("search");
        PageUtils pageUtils =  course_userService.find(currentPage,pageSize,search);
         resultVo = new ResultVo(200,"查询成功",pageUtils);
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }


//删除信息
    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] ids = req.getParameterValues("id");
       boolean b  =  course_userService.del(ids);
       if (b){
           resultVo = new ResultVo(200,"删除成功",null);
       }else {
           resultVo =  new ResultVo(500,"删除失败",null);
       }
       resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }

//查询course的信息
    public  void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Course> courseList =  courseDao.findAll();
        if (courseList!=null || courseList.size()>0){
            resultVo = new ResultVo(200,"查询成功",courseList);
        }else {
            resultVo = new ResultVo(500,"查询失败",null);
        }
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }


    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        String id = req.getParameter("id");
       boolean b = course_userService.update(cid,id);
        if (b){
            resultVo = new ResultVo(200,"修改成功",null);
        }else {
            resultVo = new ResultVo(500,"修改失败",null);
        }
        resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }
}
